from typing import Optional

import requests
from core.config import settings
from core.logger import log



class SenderTools:
    def __init__(self):
        self.base_url = settings.BASE_URL
        self.session = requests.Session()
        self.qw_send_url = "https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg"
    def send_wx_message(self, message: str, receiver_id: str, key:str) -> dict:
        url = f"{self.base_url}/message/SendTextMessage?key={key}"
        data = {
            "MsgItem": [
                {
                    "AtWxIDList": [
                        "string"
                    ],
                    "ImageContent": "",
                    "MsgType": 0,
                    "TextContent": message,
                    "ToUserName": receiver_id
                }
            ]
        }
        res_json = self.session.post(url, json=data).json()
        if res_json["Code"] != 200:
            return {"code": 400, "msg": "发送失败"}
        return {"code": 200, "msg": "发送成功", "data": message}


    def send_wx_image(self, base64_str: str, receiver_id: str, key:str) -> dict:
        url = f"{self.base_url}/message/SendImageNewMessage?key={key}"
        data = {
            "MsgItem": [
                {
                    "AtWxIDList": [
                        ""
                    ],
                    "ImageContent": base64_str,
                    "MsgType": 2,
                    "TextContent": "",
                    "ToUserName": receiver_id
                }
            ]
        }
        res_json = self.session.post(url, json=data).json()
        if res_json["Code"] != 200:
            return {"code": 400, "msg": "发送失败"}
        return {"code": 200, "msg": "发送成功", "data": res_json["Data"]}

    def send_app_messages(self,ContentXML,to_user:str,key:str):
        url = f"{self.base_url}/message/SendAppMessage?key={key}"
        data = {
            "AppList": [
                {
                    "ContentType": 49,
                    "ContentXML": ContentXML,
                    "ToUserName": to_user
                }
            ]
        }
        res_json = requests.post(url, json=data).json()
        if res_json["Code"] != 200:
            return {"code": 400, "msg": "发送失败"}
        return {"code": 200, "msg": "发送成功", "data": res_json["Data"]}

    def send_qw_welcome_message(self, access_token: str, code: str,external_userid:str,open_kfid:str):
        url = f"https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token={access_token}"
        data = {
            "code": code,
            "msgtype": "text",
            "text": {
                "content": "我们可根据您信用情况和业务需求，给出一份专业的解决方案，为了快速给您对接最合适的业务经理，您可直接说出您的需求和问题（如 我想要贷款～ 我征信有逾期应该如何处理～企业资金周转），全程2分钟搞定~ "
            }
        }

        msgmenu={
            "code":code,
            "msgtype":"msgmenu",
            "msgmenu":{
                "head_content":"完成测额 5 分钟内指派专属客户经理给您明确方案。",
                "list": [
                    # {
                    #     "type": "click",
                    #     "click": {
                    #         "id": "101",
                    #         "content": "带宽"
                    #     }
                    # },
                    # {
                    #     "type": "click",
                    #     "click": {
                    #         "id": "102",
                    #         "content": "查询"
                    #     }
                    # },
                    {
                        "type": "view",
                        "view": {
                            "url": f"http://gu.iqnn.com.cn/?external_userid={external_userid}&open_kfid={open_kfid}",
                            "content": """
                            AI数据模型测额👈👈👈
                            """

                        }
                    },
                    {
                        "type": "text",
                        "text": {
                            "content": "   \n",
                            "no_newline": 0
                        }
                    },
                    {
                        "type": "view",
                        "view": {
                            "url": f"https://v.douyin.com/89vf0fCVUNQ",
                            "content": """
                            关注抖音可优先安排👈👈👈
                            """

                        }
                    },
                    {
                        "type": "text",
                        "text": {
                            "content": "   \n",
                            "no_newline": 0
                        }
                    },
                    {
                        "type": "view",
                        "view": {
                            "url": f"https://work.weixin.qq.com/ca/cawcde0f6a79b604e9",
                            "content": "点击添加我专属经理👈👈👈"
                        }
                    }
                ]
            }
        }

        res_json = requests.post(url, json=msgmenu).json()
        log.info(res_json)
        return {"code": 200, "msg": "发送成功", "data": res_json}
    def send_qw_messages(self,access_token:str,to_user:str,open_kfid:str,content:str):

        data = {
            "touser": to_user,
            "open_kfid": open_kfid,
            # "msgid": msg_id,
            "msgtype": "text",
            "text": {
                "content": content
            }
        }
        url = f"{self.qw_send_url}?access_token={access_token}"
        res_json = requests.post(url, json=data).json()
        log.info(res_json)
        return {"code": 200, "msg": "发送成功", "data": res_json}
    def send_qw_business_card(self,access_token:str,to_user:str,open_kfid:str,card_link:str):
        url = f"{self.qw_send_url}?access_token={access_token}"
        data = {
            "touser": to_user,
            "open_kfid": open_kfid,
            "msgtype": "ca_link",
            "ca_link": {
                "link_url": card_link
            }
        }
        res_json = requests.post(url, json=data).json()
        return {"code": 200, "msg": "发送成功", "data": res_json}
